/**
 * Yolema.com Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */
package org.grayrabbit.pay.core.action.system;

import java.util.Map;
import net.sf.json.JSONObject;
import org.grayrabbit.web.struts2.GenericAction;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.grayrabbit.pay.core.model.results.GenericsResult;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.convention.annotation.Result;
import org.grayrabbit.pay.core.model.results.PayResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.grayrabbit.pay.core.model.pages.PageList;
import org.grayrabbit.pay.core.model.entity.system.AppSystem;
import org.grayrabbit.pay.core.model.entity.system.TradeTerminal;
import org.grayrabbit.pay.core.biz.manager.system.AppSystemManager;
import org.grayrabbit.pay.core.biz.manager.system.TradeTerminalManager;

/**
 * 
 * 
 * @author liyd
 * @version $Id: AppSystemAction.java, v 0.1 Fri Jun 01 11:14:01 CST 2012 liyd
 *          Exp $
 */
@Results({
          @Result(name = GenericAction.RELOAD, location = "app_system_list", type = "redirectAction"),
          @Result(name = GenericAction.INPUT, location = "app_system-input.vm"),
          @Result(name = GenericAction.LIST, location = "app_system-list.vm") })
@InterceptorRefs({ @InterceptorRef(value = "token", params = { "includeMethods", "save" }),
                  @InterceptorRef("login"), @InterceptorRef("defaultStack") })
public class AppSystemAction extends GenericAction {

    /** serialVersionUID */
    private static final long        serialVersionUID = 9024644697087393105L;

    /** 业务对象manager接口 */
    @Autowired
    private AppSystemManager         appSystemManager;

    @Autowired
    private TradeTerminalManager     tradeTerminalManager;

    /** 业务实体对象 */
    private AppSystem                appSystem        = new AppSystem();

    /** 业务实体对象列表 */
    private PageList<AppSystem>      appSystemList;
    private Map<Long, TradeTerminal> tradeTerminalMap;
    private Long[]                   terminalIds      = new Long[] {};

    /**
     * 查询列表 访问URL /app_system_list.action
     * 
     * @return
     */
    @Action(value = "app_system_list")
    public String list() {

        GenericsResult<PageList<AppSystem>> result = null;

        result = appSystemManager.queryPageList(appSystem);
        GenericsResult<Map<Long, TradeTerminal>> tradeTerminalResult = tradeTerminalManager
            .findIdMap();
        if (!result.isSuccess()) {
            this.addActionError(result.getResultMsg());
        }
        if (!tradeTerminalResult.isSuccess()) {
            this.addActionError(tradeTerminalResult.getResultMsg());
        }
        tradeTerminalMap = tradeTerminalResult.getResultData();
        appSystemList = result.getResultData();
        return LIST;
    }

    /**
     * 跳转到添加修改页面 访问URL /app_system_input.action
     * 
     * @return
     */
    @Action(value = "app_system_input")
    public String input() {
        generateDateForInput();
        if (appSystem.getSystemId() == null) {
            return INPUT;
        }
        GenericsResult<AppSystem> result = appSystemManager.getById(appSystem.getSystemId());
        if (!result.isSuccess()) {
            this.addActionError(result.getResultMsg());
        }
        appSystem = result.getResultData();

        return INPUT;
    }

    /**
     * 保存 访问URL /app_system_save.action
     * 
     * @return
     */
    @Action(value = "app_system_save")
    public String save() {

        PayResult result = null;
        if (appSystem.getSystemId() != null) {
            result = appSystemManager.update(appSystem);
            appSystemManager.deleteBySystemId(appSystem.getSystemId());
        } else {
            result = appSystemManager.insert(appSystem);
        }

        if (!result.isSuccess()) {
            this.addActionError(result.getResultMsg());
            generateDateForInput();
            return INPUT;
        } else
            appSystemManager.insertAss(terminalIds, appSystem.getSystemId());

        return RELOAD;
    }

    /**
     * 修改状态 访问URL /app_system_changeStatus.action
     * 
     * @return
     */
    @Action(value = "app_system_changeStatus")
    public String changeStatus() {

        PayResult result = appSystemManager.saveStatus(appSystem);
        JSONObject jsonObject = JSONObject.fromObject(result);
        return renderJson(jsonObject.toString());
    }

    @Action(value = "app_system_appCode")
    public String checkRepeatCode() {

        PayResult result = appSystemManager.getIfRepeatByCode(appSystem);
        String resultMsg = result.isSuccess() + "";
        return renderText(resultMsg);
    }

    /**
     * 删除 访问URL /app_system_delete.action
     * 
     * @return
     */
    @Action(value = "app_system_delete")
    public String delete() {

        PayResult result = appSystemManager.deleteBatchById(key);
        JSONObject jsonObject = JSONObject.fromObject(result);

        return renderJson(jsonObject.toString());
    }

    /**
     * 为输入准备数据
     */
    private void generateDateForInput() {
        GenericsResult<Map<Long, TradeTerminal>> tradeTerminalResult = tradeTerminalManager
            .findIdMap();

        if (!tradeTerminalResult.isSuccess()) {
            this.addActionError(tradeTerminalResult.getResultMsg());
        }
        tradeTerminalMap = tradeTerminalResult.getResultData();
        GenericsResult<Long[]> terminalIds = appSystemManager.findTerminalIds(appSystem
            .getSystemId());
        setTerminalIds(terminalIds.getResultData());

    }

    /**
     * Getter method for property <tt>appSystem</tt>.
     * 
     * @return property value of appSystem
     */
    public AppSystem getAppSystem() {
        return appSystem;
    }

    /**
     * Setter method for property <tt>appSystem</tt>.
     * 
     * @param appSystem
     *            value to be assigned to property appSystem
     */
    public void setAppSystem(AppSystem appSystem) {
        this.appSystem = appSystem;
    }

    /**
     * Getter method for property <tt>appSystemList</tt>.
     * 
     * @return property value of appSystemList
     */
    public PageList<AppSystem> getAppSystemList() {
        return appSystemList;
    }

    /**
     * Setter method for property <tt>appSystemList</tt>.
     * 
     * @param districtList
     *            value to be assigned to property appSystemList
     */
    public void setAppSystemList(PageList<AppSystem> appSystemList) {
        this.appSystemList = appSystemList;
    }

    public Long[] getTerminalIds() {
        return terminalIds;
    }

    public void setTerminalIds(Long[] terminalIds) {
        this.terminalIds = terminalIds;
    }

    public Map<Long, TradeTerminal> getTradeTerminalMap() {
        return tradeTerminalMap;
    }

    public void setTradeTerminalMap(Map<Long, TradeTerminal> tradeTerminalMap) {
        this.tradeTerminalMap = tradeTerminalMap;
    }
}
